package cn.juque.systemservice.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.juque.common.base.PageInfo;
import cn.juque.common.constants.MessageEnum;
import cn.juque.common.exception.AppException;
import cn.juque.systemservice.api.constant.GenReloadEnum;
import cn.juque.systemservice.cache.GenInfoCache;
import cn.juque.systemservice.entity.GenInfo;
import cn.juque.systemservice.mapper.GenInfoMapper;
import cn.juque.systemservice.mapper.GenMxInfoMapper;
import cn.juque.systemservice.service.IGenInfoService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author JUQUE
 * @version 1.0.0
 * <li>simple-generator</li>
 * <li></li>
 * @date 2020-04-01 20:32:42
 **/
@Slf4j
@Service("genInfoService")
public class GenInfoServiceImpl implements IGenInfoService {

    @Resource
    private GenInfoMapper genInfoMapper;

    @Resource
    private GenMxInfoMapper genMxInfoMapper;

    @Resource
    private GenInfoCache genInfoCache;

    /**
     * 分页查询
     *
     * @param pageInfo pageInfo
     * @param sysGen   sysGen
     * @return DataGrid
     */
    @Override
    public List<GenInfo> listGenWithPage(PageInfo pageInfo, GenInfo sysGen) {
        Page<GenInfo> page = new Page<>(pageInfo.getPage(), pageInfo.getLimit());
        List<GenInfo> list = genInfoMapper.selectGenList(page, sysGen);
        pageInfo.setTotal(page.getTotal());
        return list;
    }

    /**
     * 查询
     *
     * @param sysGen sysGen
     * @return List
     */
    @Override
    public List<GenInfo> listGenWithoutPage(GenInfo sysGen) {
        return genInfoMapper.selectGenList(sysGen);

    }

    /**
     * 获取自定义主键序列号
     *
     * @param prefix 前缀
     * @param date   时间
     * @return Integer
     */
    @Override
    public Long getGen(String prefix, Date date) {
        return this.genInfoCache.getGen(prefix, date);
    }

    /**
     * 获取自定义主键序列号
     *
     * @param prefix 前缀
     * @return Integer
     */
    @Override
    public Long getGen(String prefix) {
        return this.genInfoCache.getGen(prefix);
    }

    /**
     * 保存
     *
     * @param sysGen sysGen
     */
    @Override
    public void saveGen(GenInfo sysGen) {
        if (Boolean.TRUE.equals(this.isExistsPrefix(sysGen))) {
            throw new AppException(MessageEnum.PARAM_ERROR.getCode(),
                "[" + sysGen.getGenPrefix() + "]前缀已经存在");
        }
        sysGen.setReloadFlag(GenReloadEnum.WAIT.getValue());
        boolean flag = SqlHelper.retBool(this.genInfoMapper.insert(sysGen));
        if (!flag) {
            throw new AppException(MessageEnum.SYSTEM_FAIL.getCode(), "保存失败");
        }
        this.genInfoCache.put(sysGen);
    }

    /**
     * 更新
     *
     * @param sysGen sysGen
     * @throws AppException AppException
     */
    @Override
    public void updateGen(GenInfo sysGen) {
        if (Boolean.TRUE.equals(this.isExistsPrefix(sysGen))) {
            throw new AppException(MessageEnum.PARAM_ERROR.getCode(),
                "[" + sysGen.getGenPrefix() + "]前缀已经存在");
        }
        boolean flag = SqlHelper.retBool(this.genInfoMapper.updateById(sysGen));
        if (!flag) {
            throw new AppException(MessageEnum.SYSTEM_FAIL.getCode(), "更新失败");
        }
        this.genInfoCache.put(sysGen);
    }

    /**
     * 删除
     *
     * @param genIds genIds
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteGen(List<String> genIds) {

        if (CollUtil.isEmpty(genIds)) {
            return;
        }
        for (String genId : genIds) {
            GenInfo sysGen = this.genInfoMapper.selectById(genId);
            if (null != sysGen) {
                this.genInfoMapper.deleteById(genId);
                this.genMxInfoMapper.deleteByGenId(genId);
                this.genInfoCache.remove(sysGen.getGenPrefix());
            }
        }
    }

    /**
     * 校验前缀是否存在
     *
     * @param sysGen 入参
     * @return Boolean
     */
    private Boolean isExistsPrefix(@NotNull GenInfo sysGen) {
        GenInfo query = new GenInfo();
        query.setGenPrefix(sysGen.getGenPrefix());
        List<GenInfo> list = this.genInfoMapper.selectGenList(query);
        if (CollUtil.isEmpty(list)) {
            return Boolean.FALSE;
        }
        list = list.stream().filter(t -> !t.getGenId().equals(sysGen.getGenId())).collect(
            Collectors.toList());
        return CollUtil.isNotEmpty(list);
    }
}
